home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’91 / DAL Files / DALtool 6⁄6 (System 7.x) / DalHandlers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-02  |  7.4 KB  |  389 lines  |  [TEXT/KAHL]

  1. #include "DalDemo.h"
  2.  
  3. extern EventRecord    gTheEvent;
  4. extern Boolean gDone;
  5. extern char dirty;
  6. extern Rect    gDragRect, gInRect, gOutRect;
  7. extern MenuHandle    gAppleMenu, gFileMenu, gEditMenu;
  8. extern Boolean gDALActive, gDALMonitor, gInBackground;
  9. extern int gDALState;
  10. Boolean gInBackground;
  11.  
  12.         
  13. /*** HandleIdle ***/
  14. HandleIdle()
  15. {
  16.     WindowPtr    aWind;
  17.     int            aType;
  18.     
  19.     aWind = FrontWindow();
  20.     aType = (int) WindowType(aWind);
  21.  
  22.     if (aType == DEMO_WINDOW)
  23.     {
  24.         TEIdle(((demoPeek)aWind)->curTE);
  25.         if (gDALActive) DALMonitorState((demoPeek)aWind);
  26.     }
  27. }
  28.  
  29. /*** HandleEvent ***/
  30. HandleEvent()
  31. {
  32.     char theChar;
  33.     
  34.     switch (gTheEvent.what)
  35.     {
  36.         case nullEvent:
  37.             HandleIdle();
  38.             break;
  39.         case mouseDown:
  40.             HandleMouseDown();
  41.             break;
  42.         case keyDown:
  43.         case autoKey:
  44.             theChar = gTheEvent.message & charCodeMask;
  45.             if ((gTheEvent.modifiers & cmdKey) != 0)
  46.             {
  47.                 AdjustMenus();
  48.                 HandleMenuChoice(MenuKey(theChar));
  49.             }
  50.             else
  51.                 HandleTEKey(theChar);
  52.             break;
  53.         case activateEvt:
  54.             HandleActivate((WindowPtr)gTheEvent.message, (gTheEvent.modifiers & activeFlag) != 0);
  55.             break;
  56.         case updateEvt:
  57.             HandleUpdate((WindowPtr)gTheEvent.message);
  58.             break;
  59.         case app4Evt:
  60.             if ((gTheEvent.message & SUSPEND_RESUME_BIT) == RESUMING)
  61.             {
  62.                 gInBackground = (gTheEvent.message & 0x01) == 0;
  63.                 HandleActivate(FrontWindow(), !gInBackground);
  64.             }
  65.             else
  66.                 HandleIdle();
  67.             break;
  68.     }
  69. }
  70.  
  71. /*** HandleUpdate ***/
  72. HandleUpdate(w)
  73. WindowPtr w;
  74. {
  75.     GrafPtr    savePort;
  76.         
  77.     GetPort( &savePort );
  78.     SetPort( w );
  79.     BeginUpdate( w );
  80.     EraseRect(&w->portRect);
  81.     DrawDemoWind(w);
  82.     EndUpdate( w );
  83.     SetPort( savePort );
  84. }
  85.  
  86. /*** HandleMouseDown ***/
  87. HandleMouseDown()
  88. {
  89.     WindowPtr    whichWindow;
  90.     short int     thePart;
  91.     long int    menuChoice,windSize;
  92.     
  93.     thePart = FindWindow(gTheEvent.where, &whichWindow);
  94.     switch (thePart)
  95.     {
  96.         case inMenuBar:
  97.             AdjustMenus();
  98.             menuChoice = MenuSelect( gTheEvent.where);
  99.             HandleMenuChoice(menuChoice);
  100.             break;
  101.         case inSysWindow:
  102.             SystemClick(&gTheEvent,whichWindow);
  103.             break;
  104.         case inDrag:
  105.             DragWindow(whichWindow, gTheEvent.where, &gDragRect);
  106.             break;
  107.         case inGoAway:
  108.             if (TrackGoAway(whichWindow, gTheEvent.where))
  109.                 HandleCloseWindow((demoPeek)whichWindow);
  110.             break;
  111.         case inContent:
  112.             if (whichWindow != FrontWindow())
  113.                 SelectWindow(whichWindow);
  114.             else
  115.                 DoContent(whichWindow, &gTheEvent);
  116.             break;
  117.     }
  118. }
  119.  
  120. /*** AdjustCursor ***/
  121. void AdjustCursor(mouse, region)
  122. Point mouse;
  123. RgnHandle region;
  124. {
  125.     WindowPtr    aWind;
  126.     RgnHandle    arrowRgn, iBeamRgn, tmpRgn;
  127.     Rect        tmpRect;
  128.     int            aType;
  129.     GrafPtr        oldPort;
  130.     
  131.     aWind = FrontWindow();
  132.     aType = (int) WindowType(aWind);
  133.     
  134.     if (gInBackground || (aType != DEMO_WINDOW))
  135.     {
  136.         SetCursor(&arrow);
  137.         return;
  138.     }
  139.     
  140.     GetPort(&oldPort);
  141.     SetPort(aWind);
  142.     
  143.     arrowRgn = NewRgn();
  144.     iBeamRgn = NewRgn();
  145.     tmpRgn = NewRgn();
  146.     
  147.     SetRectRgn(arrowRgn, -32700, -32700, 32700, 32700);
  148.     
  149.     tmpRect = gInRect;
  150.     LocalToGlobal(&TopLeft(tmpRect));
  151.     LocalToGlobal(&BotRight(tmpRect));
  152.     RectRgn(tmpRgn, &tmpRect);
  153.     UnionRgn(iBeamRgn, tmpRgn, iBeamRgn);
  154.     
  155.     tmpRect = gOutRect;
  156.     LocalToGlobal(&TopLeft(tmpRect));
  157.     LocalToGlobal(&BotRight(tmpRect));
  158.     RectRgn(tmpRgn, &tmpRect);
  159.     UnionRgn(iBeamRgn, tmpRgn, iBeamRgn);
  160.     
  161.     DiffRgn(arrowRgn, iBeamRgn, arrowRgn);
  162.     
  163.     if (PtInRgn(mouse,iBeamRgn))
  164.     {
  165.         SetCursor(*GetCursor(iBeamCursor));
  166.         CopyRgn(iBeamRgn, region);
  167.     }
  168.     else
  169.     {
  170.         SetCursor(&arrow);
  171.         CopyRgn(arrowRgn, region);
  172.     }
  173.     
  174.     DisposeRgn(arrowRgn);
  175.     DisposeRgn(iBeamRgn);
  176.     DisposeRgn(tmpRgn);
  177.     
  178.     SetPort(oldPort);
  179. }
  180.  
  181. /*** AdjustMenus ***/
  182. AdjustMenus()
  183. {
  184.     WindowPtr    window;
  185.     int            wType;
  186.     int            offset;
  187.     TEHandle    te;
  188.     
  189.     window = FrontWindow();
  190.     wType = WindowType(window);
  191.     
  192.     if (window == NIL)
  193.     {
  194.         DisableItem(gFileMenu,CLOSE_ITEM);
  195.         CheckItem(gFileMenu, MONITOR_ITEM,FALSE);
  196.         DisableItem(gFileMenu,MONITOR_ITEM);
  197.         EnableItem(gFileMenu,NEW_ITEM);
  198.         
  199.         DisableItem(gEditMenu,UNDO_ITEM);
  200.         DisableItem(gEditMenu,CUT_ITEM);
  201.         DisableItem(gEditMenu,COPY_ITEM);
  202.         DisableItem(gEditMenu,PASTE_ITEM);
  203.         DisableItem(gEditMenu,CLEAR_ITEM);
  204.     }
  205.     else if (wType == DA_WINDOW)
  206.     {
  207.         DisableItem(gFileMenu,CLOSE_ITEM);
  208.  
  209.         EnableItem(gEditMenu,UNDO_ITEM);
  210.         EnableItem(gEditMenu,CUT_ITEM);
  211.         EnableItem(gEditMenu,COPY_ITEM);
  212.         EnableItem(gEditMenu,PASTE_ITEM);
  213.         EnableItem(gEditMenu,CLEAR_ITEM);
  214.     }
  215.     else if (wType == DEMO_WINDOW)
  216.     {
  217.         EnableItem( gFileMenu, CLOSE_ITEM);
  218.         EnableItem( gFileMenu, MONITOR_ITEM);
  219.         DisableItem( gFileMenu, NEW_ITEM);
  220.  
  221.         DisableItem(gEditMenu,UNDO_ITEM);
  222.         DisableItem(gEditMenu,CUT_ITEM);
  223.         DisableItem(gEditMenu,COPY_ITEM);
  224.         DisableItem(gEditMenu,PASTE_ITEM);
  225.         DisableItem(gEditMenu,CLEAR_ITEM);
  226.         
  227.         te = ((demoPeek)window)->curTE;
  228.         if ( (*te)->selStart < (*te)->selEnd)
  229.         {
  230.             EnableItem(gEditMenu,CUT_ITEM);
  231.             EnableItem(gEditMenu,COPY_ITEM);
  232.             EnableItem(gEditMenu,CLEAR_ITEM);
  233.         }
  234.         if ( GetScrap(NIL, 'TEXT', &offset) > 0)
  235.             EnableItem(gEditMenu,PASTE_ITEM);
  236.     }
  237. }
  238.  
  239. /*** HandleMenuChoice ***/
  240. HandleMenuChoice(menuChoice)
  241. long int menuChoice;
  242. {
  243.     int theMenu;
  244.     int theItem;
  245.     
  246.     if (menuChoice != 0)
  247.     {
  248.         theMenu = HiWord(menuChoice);
  249.         theItem = LoWord(menuChoice);
  250.         switch (theMenu)
  251.         {
  252.             case APPLE_MENU_ID:
  253.                 HandleAppleChoice(theItem);
  254.                 break;
  255.             case FILE_MENU_ID:
  256.                 HandleFileChoice(theItem);
  257.                 break;
  258.             case EDIT_MENU_ID:
  259.                 HandleEditChoice(theItem);
  260.                 break;
  261.         }
  262.         HiliteMenu(0);
  263.     }
  264. }
  265.  
  266. /*** HandleAppleChoice ***/
  267. HandleAppleChoice (theItem)
  268. int theItem;
  269. {
  270.     Str255    accName;
  271.     int        accNumber;
  272.     
  273.     switch (theItem)
  274.     {
  275.         case ABOUT_ITEM:
  276.             NoteAlert(ABOUT_ALERT, NIL);
  277.             break;
  278.         default:
  279.             GetItem(gAppleMenu, theItem, accName);
  280.             accNumber = OpenDeskAcc(accName);
  281.             break;
  282.     }
  283. }
  284.  
  285. /*** HandleFileChoice ***/
  286. HandleFileChoice(theItem)
  287. int theItem;
  288. {
  289.     WindowPtr whichWindow;
  290.     
  291.     switch ( theItem)
  292.     {
  293.         case NEW_ITEM:
  294.             CreateSession();
  295.             break;
  296.         case CLOSE_ITEM:
  297.             if ((whichWindow = FrontWindow()) != NIL)
  298.                 HandleCloseWindow((demoPeek)whichWindow);
  299.             gDALMonitor = FALSE;
  300.             break;
  301.         case MONITOR_ITEM:
  302.             gDALMonitor ^= TRUE;
  303.             CheckItem(gFileMenu, MONITOR_ITEM,gDALMonitor);
  304.             break;
  305.         case QUIT_ITEM:
  306.             if ((whichWindow = FrontWindow()) != NIL)
  307.                 HandleCloseWindow((demoPeek)whichWindow);
  308.             gDALMonitor = FALSE;
  309.             gDone = TRUE;
  310.             break;
  311.     }
  312. }
  313.  
  314. /*** HandleEditChoice ***/
  315. HandleEditChoice( theItem)
  316. int theItem;
  317. {
  318.     TEHandle    te;
  319.     WindowPtr    window;
  320.     int            wType, length, i;
  321.     CharsHandle    text;
  322.     Str255        tempStr;
  323.     demoPeek    demoWind;
  324.     
  325.     if (!SystemEdit (theItem - 1))
  326.     {
  327.         window = FrontWindow();
  328.         wType = WindowType(window);
  329.         
  330.         if (wType == DEMO_WINDOW)
  331.         {
  332.             demoWind = (demoPeek) window;
  333.             te = demoWind->curTE;
  334.             switch (theItem)
  335.             {
  336.                 case UNDO_ITEM:
  337.                     break;
  338.                 case CUT_ITEM:
  339.                     if (ZeroScrap() == noErr)
  340.                     {
  341.                         TECut(te);
  342.                         AdjustScrollBar(demoWind);
  343.                         if (TEToScrap() != noErr)
  344.                             ZeroScrap();
  345.                     }
  346.                     break;
  347.                 case COPY_ITEM:
  348.                     if (ZeroScrap() == noErr)
  349.                     {
  350.                         TECopy(te);
  351.                         if (TEToScrap() != noErr)
  352.                             ZeroScrap();
  353.                     }
  354.                     break;
  355.                 case PASTE_ITEM:
  356.                     if (TEFromScrap() == noErr)
  357.                     {
  358.                         TEPaste(te);
  359.                         AdjustScrollBar(demoWind);
  360.                     }
  361.                     break;
  362.                 case CLEAR_ITEM:
  363.                     TEDelete(te);
  364.                     AdjustScrollBar(demoWind);
  365.                     break;
  366.             } /* switch */
  367.         } /* if my wType */
  368.     } /* !SystemEdit */
  369. } /*HandleEditChoice*/
  370.  
  371. /*** ErrorHandler ***/
  372. ErrorHandler(strNum)
  373. int strNum;
  374. {
  375.     StringHandle    errStrH;
  376.     
  377.     if ((errStrH = GetString(strNum)) == NIL) 
  378.         ParamText(FATAL_STR,NIL,NIL,NIL);
  379.     else
  380.     {
  381.         HLock(errStrH);
  382.         ParamText(*errStrH,NIL,NIL,NIL);
  383.         HUnlock(errStrH);
  384.     }
  385.     StopAlert(ERROR_ALERT_ID,NIL);
  386.     ExitToShell();
  387. }
  388.  
  389.